﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using CommonUtility;
using System.Web.Script.Serialization;
using Model;

namespace BLL
{
    public class bllRoleFunction
    {
        public string GetRolePermission(string roleID, UserInfo _operator)
        {
            string result = "";
            try
            {
                //IList<RolePermissionInfo> list = RolePermission.GetList(_where);
                //string json = new JavaScriptSerializer().Serialize(list);
                IList<RoleFunctionActionInlist> list = RolePermission.GetRoleFunctionActionInlist(roleID);
                string json = new JavaScriptSerializer().Serialize(list);
                result = @"{""Rows"":" + json + @",""Total"":""" + list.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllRoleFunction.cs/GetRolePermission(" + roleID + ") " + _operator.Name, ex);
            }
            return result;
        }


        public string LoadFun(string _where, UserInfo _operator)
        {
            string result = "";
            try
            {

                IList<ActionInfo> actionInfoList = DAL.Action.GetList(" [flag]='A'");
                IList<FunctionInfo> functionInfoList = Function.GetList(" [flag]='A'");
                int j = 0;
                StringBuilder strAppend = new StringBuilder();
                strAppend.Append("[");
                foreach (FunctionInfo fun in functionInfoList)
                {
                    string actDesc = FunctionAction.GetActDesc(fun.FunID);
                    string actID = FunctionAction.GetActID(fun.FunID);
                    strAppend.Append("{");
                    strAppend.Append("'RID':'" + fun.RID + "',");
                    strAppend.Append("'FunID':'" + fun.FunID + "',");
                    strAppend.Append("'FunNm':'" + fun.FunDesc.Replace('\'', '&') + "',");
                    strAppend.Append("'Statuslst':'" + actDesc + "',");
                    string[] arrPer = actDesc.Split('/');
                    string[] arrActID = actID.Split('/');
                    for (int i = 0; i < arrPer.Length; i++)
                    {
                        strAppend.Append("'Permission" + (i + 1) + "':'" + arrPer[i] + "',");
                        strAppend.Append("'ActID" + (i + 1) + "':'" + arrActID[i] + "'");
                        if (i < actionInfoList.Count - 1)
                            strAppend.Append(",");
                    }
                    for (int k = arrPer.Length; k < actionInfoList.Count; k++)
                    {
                        strAppend.Append("'Permission" + (k + 1) + "':'',");
                        strAppend.Append("'ActID" + (k + 1) + "':''");
                        if (k < actionInfoList.Count - 1)
                            strAppend.Append(",");
                    }

                    strAppend.Append("}");
                    if (j < functionInfoList.Count - 1)
                        strAppend.Append(",");
                    j++;
                }
                strAppend.Append("]");
                string json = new JavaScriptSerializer().Serialize(strAppend.ToString());
                result = @"{""Rows"":" + json + @",""Total"":""" + functionInfoList.Count + @"""}";
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllRoleFunction.cs/LoadFun('" + _where + "')" + _operator.Name, ex);
            }
            return result;
        }


        public string LoadColumn(UserInfo _operator)
        {
            string result = "";
            try
            {
                int i = 1;
                StringBuilder strAppend = new StringBuilder();
                strAppend.Append("[");

                strAppend.Append("{");
                strAppend.Append("display: 'Select', name: 'Permit', align: 'left', width: 60, minWidth: 60, isAllowHide: false,");
                strAppend.Append(" render:function (rowdata) {var iconHtml=\'<div class=\"access-icon access-permit\'; ");
                strAppend.Append("if(rowdata.Permit) iconHtml+=\" access-icon-selected\"; ");
                strAppend.Append("iconHtml+=\'\"\'; ");
                strAppend.Append("iconHtml+=' rowid=\"\' +rowdata['__id']+ \'\"\'; ");
                strAppend.Append("iconHtml+=\'></div>'; ");
                strAppend.Append("return iconHtml;}");
                strAppend.Append("},");

                strAppend.Append("{");
                strAppend.Append("display: 'Function Name', name: 'FunNm', align: 'left', width: 200, minWidth: 60, render: function(rowdata,rowindex,value){return rowdata.FunNm.replace('&','&acute;');}");
                strAppend.Append("},");

                for (int j = 0; j < FunctionAction.GetMaxFunLen(); j++)
                {
                    strAppend.Append("{");
                    strAppend.Append("display: 'Permission" + i + "', name: 'Permission" + i + "', align: 'left', width: 80, minWidth: 50,");
                    strAppend.Append("render:function(rowdata,rowindex,value){ var h=\"<a id=\"+rowdata.ActID" + i + " +\" href=\'javascript:PermClick(this)\'>\"+rowdata.Permission" + i + " +\"</a>\"; return h;}");
                    strAppend.Append("}");
                    if (i < FunctionAction.GetMaxFunLen())
                        strAppend.Append(",");
                    i++;
                }
                strAppend.Append("]");
                result = strAppend.ToString();
            }
            catch (Exception ex)
            {
                result = "ERR";
                Common.Application_Error_Server("BLL/bllRoleFunction.cs/LoadCloumn() " + _operator.Name, ex);
            }
            return result;
        }


        public string DataSave(string _funID, string _dataJSON, string _roleID, UserInfo _operator)
        {
            string res = string.Empty;
            System.Data.SqlClient.SqlTransaction myTran = SQLCommon.GetSqlTransaction();
            System.Data.SqlClient.SqlConnection myConn = myTran.Connection;
            try
            {
                string remark = string.Empty;
                var data = new JavaScriptSerializer().Deserialize<List<RoleFunctionActionInlist>>(_dataJSON);
                IList<RolePermissionInfo> cls = RolePermission.GetList(" [roleID] = '" + _roleID + "'");
                foreach (var rlpms in cls)
                {
                    RolePermission.Delete(rlpms);
                }

                foreach (RoleFunctionActionInlist rfa in data)
                {
                    if (string.IsNullOrEmpty(rfa.ActioinLst.Trim()))
                    {
                        continue;
                    }
                    else
                    {
                        string[] actArray = rfa.ActioinLst.Split('/');//string[]
                        foreach (string act in actArray)
                        {
                            RolePermissionInfo cls2 = new RolePermissionInfo();
                            cls2.RoleID = rfa.RoleID.Trim();
                            cls2.FunID = rfa.FunID.Trim();
                            cls2.ActID = act.Trim();//
                            cls2.Whom = _operator.UserID;
                            cls2.Flag = "A";
                            RolePermission.Add(cls2, myTran);
                        }
                    }
                    //添加操作记录
                    BLL.bllLog.AddLog(_funID, "Save", "RoleID:" + rfa.RoleID + " FunID:" + rfa.FunID + " Actioin List:" + rfa.ActioinLst, _operator.UserID, myTran);
                }

                


                myTran.Commit();
            }
            catch (Exception ex)
            {
                myTran.Rollback();
                Common.Application_Error_Server("BLL/bllRoleFunction.cs/DataSave() " + _operator.Name, ex);
                res = ex.Message;
            }
            finally
            {
                myConn.Close();
                myTran.Dispose();
                myConn.Dispose();
            }

            return res;
        }
    }
}
